package org.example.controller;

import com.github.pagehelper.PageInfo;
import org.example.entity.NoteTag;
import org.example.service.NoteTagService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 谢娅
 */
@RestController
@RequestMapping("/noteTag")
public class NoteTagController {

    @Resource
    private NoteTagService noteTagService;

    @PostMapping("/save")
    public Map<String,Object> save(@RequestBody NoteTag noteTag) {
        if(noteTag.getNoteId() == null || noteTag.getTagId() == null){
            return Result.fileMap("Note id or Tag id cannot be empty");
        }
        return Result.successMap("NoteTag saved successfully");
    }




    @DeleteMapping("/remove/{id}")
    public Map<String,Object> remove(@PathVariable Serializable id) {
        NoteTag noteTag = noteTagService.getById(id);
        if (noteTag == null){
            return Result.fileMap("NoteTag does not exist");
        }
        return Result.successMap("NoteTag removed successfully");
    }

    @PutMapping("/update")
    public Map<String,Object> update(@RequestBody NoteTag noteTag) {
        if(noteTag.getNoteTagId() == null){
            return Result.fileMap("NoteTag id cannot be empty");
        }
        if(noteTag.getNoteId() == null || noteTag.getTagId() == null){
            return Result.fileMap("Note id or Tag id cannot be empty");
        }
        boolean b = noteTagService.updateById(noteTag);
        if(!b){
            return Result.successMap("NoteTag update failed");
        }
        return Result.successMap("NoteTag updated successfully");
    }

    @GetMapping("/list")
    public Map<String,Object> list() {
        Map<String,Object> map = new HashMap<>();
        map.put("noteTagList",new PageInfo<>(noteTagService.list()));
        return Result.successMap(map);
    }

    @GetMapping("/getInfo/{id}")
    public Map<String,Object> getInfo(@PathVariable Serializable id) {
        return Result.successMap(noteTagService.getById(id));
    }
}